package com.zwh.config.database;

import com.baomidou.mybatisplus.core.MybatisConfiguration;
import com.baomidou.mybatisplus.core.MybatisXMLLanguageDriver;
import com.baomidou.mybatisplus.core.config.GlobalConfig;
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import com.zwh.config.MyBatisPlusHandler;
import com.zwh.config.SlowSqlInterceptor;
import com.zwh.config.SqlInterceptor;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.type.JdbcType;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;


import javax.sql.DataSource;

/**
 * 类
 *
 * @author jiangxianliang
 * @since 2021/5/12
 */
@EnableTransactionManagement
@Configuration
@MapperScan(basePackages = "com.zwh.mapper", sqlSessionTemplateRef = "masterSqlSessionTemplate")
public class MasterDataSourceConfig {

	@Autowired
	@Qualifier(DataSourceConfig.DATASOURCE_MASTER_BEAN_NAME)
	private DataSource masterDataSource;

	@Autowired
	private MyBatisPlusHandler myBatisPlusHandler;

	@Primary
	@Bean("masterSqlSessionFactory")
	public SqlSessionFactory masterSqlSessionFactory(SlowSqlInterceptor slowSqlInterceptor, SqlInterceptor sqlInterceptor) throws Exception {
		MybatisSqlSessionFactoryBean factoryBean = new MybatisSqlSessionFactoryBean();
		factoryBean.setDataSource(masterDataSource);

		MybatisConfiguration configuration = new MybatisConfiguration();
		configuration.setDefaultScriptingLanguage(MybatisXMLLanguageDriver.class);
		configuration.setJdbcTypeForNull(JdbcType.NULL);
		configuration.setLogImpl(org.apache.ibatis.logging.stdout.StdOutImpl.class);

		factoryBean.setConfiguration(configuration);
		//指定xml路径.
		factoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:com/zwh/mapper/*Mapper.xml"));
		factoryBean.setTypeAliasesPackage("com.zwh.entity");
		factoryBean.setPlugins(new Interceptor[]{
			myBatisPlusHandler.initMybatisPlusInterceptor()
		});

		GlobalConfig globalConfig = new GlobalConfig();
		globalConfig.setMetaObjectHandler(myBatisPlusHandler);
		factoryBean.setGlobalConfig(globalConfig);
		// 添加拦截器,注册多个拦截器注意顺序！先执行的拦截器先注册
		factoryBean.setPlugins(slowSqlInterceptor,sqlInterceptor);
		return factoryBean.getObject();
	}


	@Primary
	@Bean("masterSqlSessionTemplate")
	public SqlSessionTemplate masterSqlSessionTemplate(@Qualifier("masterSqlSessionFactory") SqlSessionFactory masterSqlSessionFactory) {
		SqlSessionTemplate template = new SqlSessionTemplate(masterSqlSessionFactory);
		return template;
	}

	@Primary
	@Bean("jdbcTemplate")
	public JdbcTemplate jdbcTemplate() throws Exception {
		return new JdbcTemplate(masterDataSource);
	}

	@Primary
	@Bean("masterDataSourceTransactionManager")
	public DataSourceTransactionManager dataSourceTransactionManager() {
		return new DataSourceTransactionManager(masterDataSource);
	}

}
